<?php
	if(!isset($_SESSION)){
		session_start();
	}
	
	// Clear previous messages.
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');

	// Get values from the form.
	$unitcode = $_POST['unitcode'];
	$unitDescription = $_POST['unitDescription'];
	
	// Check for validation.
	if($unitcode == "" ){
		$_SESSION['error'] = "Pleace complete the mandatory fields";		
	}else{
		include("../dbconnection.php");
		if($result = mysql_query("SELECT * FROM units WHERE unitcode='$unitcode'")){
			if(mysql_num_rows($result) > 0){
				$row = mysql_fetch_assoc($result);
				$_SESSION['error'] = "Unit ".$row['unitcode']." is already exisist.";
			}else{
				if(mysql_query("INSERT INTO units (unitcode, unitDescription) VALUES ('$unitcode','$unitDescriptions')")){
					$_SESSION['success'] = "Unit added successfully.";
				}else{
					$_SESSION['error'] = "Error occured!";
				}
			}
		}else{
			$_SESSION['error'] = "Error occured!";
		}
	}
	$_SESSION['page'] = "addUnits.php";
	header('Location: ../../');
?>